#include <stdio.h>
#include <stdlib.h>
#include "../../src/thread.h"

my_sem_t mutex;

void* fils (void* arg){
  int i;
  int duree = *((int*)arg); 
  for(i=0; i<duree; i++) {
    printf("%d - fils ... (%d)\n",thread_self(),duree-i);   
    thread_yield();  
  }
  printf("%d  -   le fils a terminé\n",thread_self());
  thread_exit(NULL);
} 

void* pere (void* arg){
  int i;
  thread_t f1; thread_t f2;
  int d = *((int*)arg);
  int d1;
  int d2;
  
  printf("         fils1:");
  scanf("%d",&d1);
  printf("         fils2:");
  scanf("%d",&d2);
  printf("\n\n");

  thread_create(&f1, fils,&d1);
  thread_create(&f2, fils,&d2);

  for(i=0; i<d; i++) {
    printf("%d - pere ... (%d)\n",thread_self(),d-i);
    thread_yield();  
  }

  printf("%d  -   Pere a terminé\n",thread_self());
  thread_exit(NULL);
} 


int main (void){
  int i; 
  thread_t tid_pere;
  int dmain;
  int dpere;
  
  printf("Les durées:\n");
  printf("   main:");
  scanf("%d",&dmain);
  printf("      pere:");
  scanf("%d",&dpere);

  if (thread_create(&tid_pere, pere, &dpere)) { 
    perror("thread_create"); 
    exit(EXIT_FAILURE); 
  } 
  
  for(i=0; i<dmain; i++) {
    printf("%d - main ... (%d)\n",thread_self(),dmain-i);    
    thread_yield();  
  }

  printf("%d  -   Le main a terminé\n", thread_self());
  killMainThread();
  return (EXIT_SUCCESS);
}
